#include <stdio.h>
#include <time.h>
#include <iostream>


#include "SysLog.h"
#include "SysInfo.h"
#include "NetComm.h"
#include "SysCtrl.h"
extern "C"{
#include "CommonShare.h"
#include "ShowImage.h"
}



void* routine(void * arg);
void* touchScreenFunc(void * arg);
void* showImageFunc(void *arg);

int main(int argc,char *argv[])
{
    printf("Hello Polaris !\n");
/*
    printf("sizeof(int) = %d\n",sizeof(int));
    printf("sizeof(char) = %d\n",sizeof(char));
    printf("sizeof(short) = %d\n",sizeof(short));
    printf("sizeof(long) = %d\n",sizeof(long));
    printf("sizeof(float) = %d\n",sizeof(float));
    printf("sizeof(double) = %d\n",sizeof(double));
    printf("sizeof(long long) = %d\n",sizeof(long long));
*/
//blog: https://www.cnblogs.com/baiduboy/p/7287026.html

    SysLog tmp_SysLog;
    tmp_SysLog.SysLogOut("from main() calling LogOut %d + %d = %d\n",2,3,2+3);
    SysInfo tmp_SysInfo;
    init_share();
    int scale = 1;
    printf("argc = %d\n",argc);
    if(argc>1)
        scale = atoi(argv[1]);
    writeShmArray(scale);
    char devName[128];
    char devIp[128];
    char freqBuf[100];
    printf("before while()\n");
    
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

    pthread_t netThread;
    pthread_create(&netThread,&attr,routine,NULL);


    pthread_t HmiTouchView;
    pthread_create(&HmiTouchView,NULL,touchScreenFunc,NULL);

    ArmSysInfo *tmp_ArmSysInfo = (ArmSysInfo *)get_pShmArmSysInfo();


    pthread_t showImage_tid;
    pthread_create(&showImage_tid,NULL,showImageFunc,NULL);
     
    
    while(1)
    {
        
        tmp_SysInfo.getDevName(devName,sizeof(devName));
        tmp_SysInfo.getDevIpAddr(devIp,sizeof(devIp));
        tmp_SysInfo.getCurrFreq(freqBuf,sizeof(freqBuf));
        

        strncpy(tmp_ArmSysInfo->DevName,devName,sizeof(tmp_ArmSysInfo->DevName));
        strncpy(tmp_ArmSysInfo->IpAddr,devIp,sizeof(tmp_ArmSysInfo->IpAddr));
        strncpy(tmp_ArmSysInfo->DevtimeStr,tmp_SysLog.GetTimeStrNow(),sizeof(tmp_ArmSysInfo->DevtimeStr));
        strncpy(tmp_ArmSysInfo->CurrFreq,freqBuf,sizeof(tmp_ArmSysInfo->CurrFreq));


        //printf("main tmp_ArmSysInfo->DevName = %s\n",tmp_ArmSysInfo->DevName);
        //printf("main tmp_ArmSysInfo->IpAddr = %s\n",tmp_ArmSysInfo->IpAddr);
        //printf("main tmp_ArmSysInfo->DevtimeStr = %s\n",tmp_ArmSysInfo->DevtimeStr);
        //printf("main tmp_ArmSysInfo->CurrFreq = %s\n",tmp_ArmSysInfo->CurrFreq);

        sleep(1);
        
    }

    dest_share();

    


    return 0;
}


void * routine(void * arg)
{
    NetComm tmp_NetComm;
    // 这个会堵塞
    tmp_NetComm.socketAccept();
}


void* touchScreenFunc(void *arg)
{
    // 设置为分离属性，结束时系统自动回收资源
    pthread_detach(pthread_self());
    //SysCtrl
    SysCtrl tmp_SysCtrl;

    tmp_SysCtrl.HMIinit();
    printf("over touchScreenFunc()\n");
}


void* showImageFunc(void *arg)
{
    ShowImage tmp_ShowImage;
    /*
    char imageFiles[][50] = {
        {"/IOT/jpg_photo/gaoyuanyuan800_480.jpg"},
        {"/IOT/jpg_photo/basketball800_480.jpg"},
        {"/IOT/jpg_photo/harvard.jepg"},
        {"/IOT/jpg_photo/3.jpg"},
        {}
    };
    */
   
    while(1)
    {
        /*
        for(int i=0;i<sizeof(imageFiles)/sizeof(imageFiles[0]);i++)
        {
            tmp_ShowImage.showPhoto(imageFiles[i]);
            sleep(5);
        }
        */

       tmp_ShowImage.showAllPhotoInDir("/root/PolarisProject/photos",1);
    }    

}